package com.geek.projects.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Map;

public class MapToObjectUtil {

    public static <T> T mapToObjectUtil(Map<String, Object> params, T t) throws IllegalAccessException {
        if (params == null || params.isEmpty()) {
            return null;
        }
        Class<?> aClass = t.getClass();
        Field[] fields = aClass.getDeclaredFields();
        for (Field field : fields) {
            int mod = field.getModifiers();
            if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
                continue;
            }
            field.setAccessible(true);
            if (params.containsKey(field.getName())) {
                field.set(t, params.get(field.getName()));
            }
        }
        return t;
    }
}
